package model

import (
	"database/sql"
	"gicechina/help"
)

type Job struct {
	Id          int    `json:"id"`
	CategoryId  int    `json:"category_id"`
	Title       string `json:"title"`
	Keyword     string `json:"keyword"`
	Description string `json:"description"`
	ScanNum     int    `json:"scan_num"`
	Operator    string `json:"operator"`
	UpdateTime  string `json:"update_time"`
	Content     string `json:"content"`
	Status      int    `json:"status"`
	Cname       string `json:"cname"`
}

//获取数据
func (c *Job) Item(params map[string]interface{}) []Job {
	var news []Job
	var result *sql.Rows
	var err error
	var sql string
	sql = "SELECT id,title,category_id,keyword,description,scan_num,operator,update_time,content,status FROM t_job WHERE status != 3"
	if _, ok := params["status"]; ok {
		sql += " AND status = " + params["status"].(string)
	}
	if _, ok := params["category_id"]; ok {
		sql += " AND category_id=" + params["category_id"].(string)
	}
	if _, ok := params["title"]; ok {
		sql += " AND title like '%" + params["title"].(string) + "%'"
	}
	if _, ok := params["starttime"]; ok {
		sql += " AND update_time >= '" + params["starttime"].(string) + "'"
	}
	if _, ok := params["endtime"]; ok {
		sql += " AND update_time <= '" + params["endtime"].(string) + "'"
	}
	if _, ok := params["sort"]; ok {
		sql += " ORDER BY " + params["sort"].(string)
	}
	if _, ok := params["limit"]; ok {
		sql += " LIMIT " + params["limit"].(string)
	}
	result, err = help.Db.Query(sql)
	if err != nil {
		help.CheckError(err, "获取招聘数据失败")
		return news
	}
	defer result.Close()
	for result.Next() {
		var new Job
		result.Scan(&new.Id, &new.Title, &new.CategoryId, &new.Keyword, &new.Description, &new.ScanNum, &new.Operator, &new.UpdateTime, &new.Content, &new.Status)
		//获取文章栏目名称
		var cname string
		err1 := help.Db.QueryRow("select title from t_category where id=?", new.CategoryId).Scan(&cname)
		if err1 != nil {
			help.CheckError(err1, "根据栏目id获取栏目信息失败")
			cname = "未知栏目"
		}
		new.Cname = cname
		news = append(news, new)
	}
	return news
}

//获取招聘总数
func (c *Job) Count(params map[string]interface{}) int {
	var err error
	var sql string
	total := 0
	sql = "SELECT count(id) as total FROM t_job WHERE status != 3"
	if _, ok := params["category_id"]; ok {
		sql += " AND category_id=" + params["category_id"].(string)
	}
	if _, ok := params["title"]; ok {
		sql += " AND title like '%" + params["title"].(string) + "%'"
	}
	if _, ok := params["status"]; ok {
		sql += " AND status = " + params["status"].(string)
	}
	if _, ok := params["category_id"]; ok {
		sql += " AND category_id = " + params["category_id"].(string)
	}
	if _, ok := params["starttime"]; ok {
		sql += " AND update_time >= '" + params["starttime"].(string) + "'"
	}
	if _, ok := params["endtime"]; ok {
		sql += " AND update_time <= '" + params["endtime"].(string) + "'"
	}
	if _, ok := params["sort"]; ok {
		sql += " ORDER BY " + params["sort"].(string)
	}
	if _, ok := params["limit"]; ok {
		sql += " LIMIT " + params["limit"].(string)
	}
	err = help.Db.QueryRow(sql).Scan(&total)
	if err != nil {
		help.CheckError(err, "获取招聘总数失败")
	}
	return total
}

//根据id获取招聘信息
func (c *Job) GetArticleInfoById(id int) (new Job, err error) {
	err = help.Db.QueryRow("SELECT id,title,category_id,keyword,description,scan_num,operator,update_time,content,status FROM t_job WHERE id=?", id).Scan(&new.Id, &new.Title, &new.CategoryId, &new.Keyword, &new.Description, &new.ScanNum, &new.Operator, &new.UpdateTime, &new.Content, &new.Status)
	if err != nil {
		help.CheckError(err, "根据招聘id获取招聘失败")
		return new, err
	}
	return new, err
}
//将浏览数增加1
func (c *Job) AddScanNum(id int) {
	_,err := help.Db.Exec("UPDATE t_job SET scan_num = scan_num + 1 WHERE id =?", id)
	if err != nil {
		help.CheckError(err,"招聘浏览数增加1失败")
	}
}